<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb>
      <el-breadcrumb-item>
        <i class="el-icon-s-home"></i>
        首页
      </el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 头部导航 -->
    <div class="tab">
      <router-link to="/home">
        <i class="el-icon-arrow-left" style="font-size: 20px; font-weight: bold; color: #000;"></i>
      </router-link>
      <p style="font-size: 14px; font-weight: bold;margin-left: 20px;">商品推荐</p>
    </div>
    <!-- 主体 -->
    <div class="content">
      <div style="padding: 20px 80px;">
        <el-alert style="width: 80%;" title="提示:请在下方选择要显示的商品;不选择则不显示" type="success" effect="dark">
        </el-alert>
        <div style="padding: 20px;">
          <button class="hui" @click="setZZ">
            <button class="btn">选择商品</button>
          </button>
          <!-- 商品展示 -->
          <div class="shopShow">
            <div class="item" v-for="item in reList" :key="item.id">
              <div style="display: flex;">
                <div style="width: 132px;">
                  <img class="itemImg" :src="item.img" alt="">
                  <p class="name">{{ item.name }}</p>
                </div>
                <i @click="del(item.id)" class="el-icon-circle-close"
                  style="font-size: 22px;margin-top: -10px; margin-left: -10px;"></i>
              </div>
            </div>
          </div>
        </div>
        <!-- 遮罩层 -->
        <div class="zz" v-show="flag">
          <div class="set">
            <!-- 头部 -->
            <div class="setTop">
              <p>选择商品</p>
              <i class="el-icon-close" style="font-size: 20px;font-weight: bold;" @click="setclose"></i>
            </div>
            <!-- 搜索 -->
            <div style="padding: 20px;">
              <div class="seachdiv">
                <p style="margin-top: 5px;">药品名称</p>
                <div>
                  <input type="text" v-model="search" class="ipt">
                  <button class="btn" style="padding: 0 20px;" @click="seaBtn">搜索</button>
                </div>
              </div>
              <!-- 表格 -->
              <div class="gun">
                <table>
                  <th>商品</th>
                  <th>名字</th>
                  <th>操作</th>
                  <tr v-for="item in shopList" :key="item.id">
                    <td>
                      <img style="width: 30px; height: 30px;" :src="item.img" alt="">
                    </td>
                    <td>{{ item.name }}</td>
                    <td>
                      <button class="chose1" @click='no(item)' v-if="reList.some(i => i.name == item.name)">取消</button>
                      <button class="chose" @click="ok(item)" v-else>选择</button>
                    </td>
                  </tr>
                </table>
              </div>


            </div>
            <div class="btnDiv">
              <button @click="setOk">确定</button>
              <button @click="setclose">取消</button>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import { getZwyShopListBx, getShopReBx, AddShopReBx, DelShopReBx } from '../../utils/api'
export default {
  data() {
    return {
      flag: false,
      search: '',
      shopList: [],
      reList: []
    }
  },
  created() {
    this.shopRen()
    this.reRen()
  },
  mounted() {
    this.shopRen()
    this.reRen()
  },
  methods: {
    //搜索
    seaBtn() {
      const jg = this.shopList.filter(item => {
        return item.name.includes(this.search)
      })
      this.shopList = jg
      console.log(jg);
    },
    setZZ() {
      this.flag = true
      this.shopRe
    },
    shopRen() {
      getZwyShopListBx().then(res => {
        this.shopList = res.data.data
      })

    },
    reRen() {
      getShopReBx().then(res => {
        this.reList = res.data.data
      })
    },
    setclose() {
      this.flag = false
    },
    // 确认
    setOk() {
      this.flag = false
      this.reRen()
    },
    // 选择
    ok(i) {
      AddShopReBx({ img: i.img, name: i.name }).then(res => {
        console.log(res);
        if (res.data.code == 200) {
          this.reRen()
        }
      })
    },
    no(i) {
      const sj = this.reList.find(item => {
        return item.name == i.name
      })
      DelShopReBx({ id: sj.id }).then(res => {
        console.log(res);
        if (res.status == 200) {
          this.reRen()
        }
      })

    },
    del(i) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        DelShopReBx({ id: i }).then(res => {
          console.log(res);
          if (res.status == 200) {
            this.reRen()
            this.$message({
              type: 'success',
              message: '删除成功!'
            });
          }
        })

      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });

    }

  }
}
</script>
<style scoped>
.gun {
  height: 250px;
  width: 100%;
  overflow: auto;
  /* overflow-y: hidden; */
}

.btnDiv button {
  border: 1px solid rgba(187, 187, 187, 1);
  color: rgba(16, 16, 16, 1);
  font-size: 14px;
  background-color: #fff;
  border-radius: 5px;
  width: 50px;
  height: 30px;
}

.btnDiv {
  /* margin-left: 100px; */
  width: 30%;
  display: flex;
  margin: 30px auto;
  justify-content: space-between;
}

.chose1 {
  width: 50px;
  height: 27px;
  line-height: 27px;
  /* padding: 0 10px; */
  background-color: red;
  color: #fff;
  border-radius: 3px;
  border: none;
}

.chose {
  width: 50px;
  height: 27px;
  line-height: 27px;
  /* padding: 0 10px; */
  background-color: #0790F5;
  color: #fff;
  border-radius: 3px;
  border: none;
}

table {
  margin-top: 30px;
  border: 1px solid #777777;
  border-collapse: collapse;
  text-align: center;
  width: 100%;
  font-size: 14px;
}

tr {
  height: 40px;
}

td {
  width: 100px;
  border: 1px solid #777777;
}

th {
  height: 50px;
  line-height: 50px;
  font-weight: normal;
  background-color: #F6F6F6;
  border: 1px solid #777777;
}

.btn {
  height: 30px;
  border: 1px solid rgba(187, 187, 187, 1);
  border-radius: 3px;
  background-color: #fff;
  /* padding: 0 20px; */
}

.ipt {
  width: 400px;
  height: 30px;
  border: 1px solid rgba(187, 187, 187, 1);
  border-radius: 3px;
}

.seachdiv {
  display: flex;
  justify-content: space-between;
  width: 85%;

}

.setTop {
  padding: 18px 22px;
  display: flex;
  justify-content: space-between;
  border-bottom: 1px solid #a5a5a5;
}

.set {
  font-size: 14px;
  width: 50%;
  height: 500px;
  position: absolute;
  top: 50%;
  left: 50%;
  -webkit-transform: translate(-50%, -50%);
  transform: translate(-50%, -50%);
  background: #ffffff;
  border-radius: 5px;

}

.zz {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  background-color: rgba(0, 0, 0, 0.5);
}

.name {
  width: 100%;
  background-color: #616161;
  line-height: 34px;
  color: rgba(255, 255, 255, 1);
  font-size: 14px;
  height: 34px;
  text-align: center;
}

.itemImg {
  width: 100%;
  height: calc(120px - 34px);
}

.item {
  width: 132px;
  height: 120px;
  margin-right: 50px;
  margin-bottom: 20px;
}

.shopShow {
  margin-top: 30px;
  width: 80%;
  flex-wrap: wrap;
  display: flex;
  justify-content: start;
}

.btn {
  height: 30px;
  float: right;
  color: #101010;
  border: 1px solid rgba(187, 187, 187, 1);
  padding: 0 10px;
  border-radius: 3px;
}

.hui {
  width: 718px;
  height: 30px;
  background-color: rgba(187, 187, 187, 1);
  border: none;
}

.content {
  width: 100%;
  height: 100%;
}

.tab {
  padding: 20px 40px;
  display: flex;
}
</style>